<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="从零开始写一个PHP框架"/>




  <meta name="keywords" content="技术, PHP, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/create-a-php-framework.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> 从零开始写一个PHP框架 - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          从零开始写一个PHP框架
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2015-05-24
        </span>
        
          <div class="post-category">
            
              <a href="/categories/技术/">技术</a>
            
          </div>
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">Contents</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#开始"><span class="toc-text">开始</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#设置index-php-入口以及路由"><span class="toc-text">设置index.php 入口以及路由</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#创建测试控制器"><span class="toc-text">创建测试控制器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#提取父类控制器"><span class="toc-text">提取父类控制器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Model"><span class="toc-text">Model</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#连接数据库"><span class="toc-text">连接数据库</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p><em>相关代码在这里：github.com/zakokun/myPHP</em></p>
<p>个人觉得徒手写一个PHP框架是帮助我们了解MVC架构的好办法，同时对PHP的资源调度以及代码组织都是一个很好的锻炼。</p>
<a id="more"></a>
<h1 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h1><p>首先我们建立基本的文件夹结构，如下：</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">application</div><div class="line">——controller</div><div class="line">——model</div><div class="line">——view</div><div class="line">system</div><div class="line">index.php</div></pre></td></tr></table></figure>
<p><code>application</code>是项目的目录，而system不用说就是框架文件的目录了。<br><code>index.php</code> 是我们所有请求的入口，我们规定通过如下的url来调用<code>controller</code>：<code>localhost/index.php?c=controller&amp;a=action&amp;param=123</code></p>
<h1 id="设置index-php-入口以及路由"><a href="#设置index-php-入口以及路由" class="headerlink" title="设置index.php 入口以及路由"></a>设置index.php 入口以及路由</h1><p>因为所有的请求都是通过<code>index.php</code>发起的，因此我们需要在<code>index.php</code>里面进行一些初始化的设置：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">define(<span class="string">'BASE_ROOT'</span>, dirname(<span class="keyword">__FILE__</span>) . DIRECTORY_SEPARATOR);</div><div class="line">define(<span class="string">'APP_ROOT'</span>, BASE_ROOT . <span class="string">'application'</span> .  DIRECTORY_SEPARATOR);</div></pre></td></tr></table></figure>
<p>因为我们规定url参数中的<code>c</code>是控制器名，<code>a</code>是方法名，因此<code>index.php</code>中需要一个路由函数去活取这两个参数：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">define(<span class="string">'BASE_ROOT'</span>, dirname(<span class="keyword">__FILE__</span>) . DIRECTORY_SEPARATOR);</div><div class="line">define(<span class="string">'APP_ROOT'</span>, BASE_ROOT . <span class="string">'application'</span> .  DIRECTORY_SEPARATOR);</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">route</span><span class="params">()</span></span>&#123;</div><div class="line">    $c = <span class="keyword">isset</span>($_GET[<span class="string">'c'</span>])?$_GET[<span class="string">'c'</span>]:<span class="string">'index'</span>;</div><div class="line">    $a = <span class="keyword">isset</span>($_GET[<span class="string">'a'</span>])?$_GET[<span class="string">'a'</span>]:<span class="string">'index'</span>;</div><div class="line"></div><div class="line">    $r[<span class="string">'c'</span>]=ucfirst(strtolower(trim($c)));</div><div class="line">    $r[<span class="string">'a'</span>]=strtolower(trim($a));</div><div class="line">    <span class="keyword">return</span> $r;</div><div class="line">&#125;</div><div class="line">$routeArr = route();</div><div class="line">$controller=$routeArr[<span class="string">'c'</span>];</div><div class="line">$action=$routeArr[<span class="string">'a'</span>];</div></pre></td></tr></table></figure>
<p>获取到了需要实例化的controller以后，就可以进行相关操作了：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">define(<span class="string">'BASE_ROOT'</span>, dirname(<span class="keyword">__FILE__</span>) . DIRECTORY_SEPARATOR);</div><div class="line">define(<span class="string">'APP_ROOT'</span>, BASE_ROOT . <span class="string">'application'</span> .  DIRECTORY_SEPARATOR);</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">route</span><span class="params">()</span></span>&#123;</div><div class="line">    $c = <span class="keyword">isset</span>($_GET[<span class="string">'c'</span>])?$_GET[<span class="string">'c'</span>]:<span class="string">'index'</span>;</div><div class="line">    $a = <span class="keyword">isset</span>($_GET[<span class="string">'a'</span>])?$_GET[<span class="string">'a'</span>]:<span class="string">'index'</span>;</div><div class="line"></div><div class="line">    $r[<span class="string">'c'</span>]=ucfirst(strtolower(trim($c)));</div><div class="line">    $r[<span class="string">'a'</span>]=strtolower(trim($a));</div><div class="line">    <span class="keyword">return</span> $r;</div><div class="line">&#125;</div><div class="line">$routeArr = route();</div><div class="line">$controller=$routeArr[<span class="string">'c'</span>];</div><div class="line">$action=$routeArr[<span class="string">'a'</span>];</div><div class="line"></div><div class="line"><span class="comment">//根据route的参数加载所需的文件</span></div><div class="line"><span class="keyword">if</span> (file_exists(APP_ROOT . <span class="string">'controller/'</span> . $controller.<span class="string">'Controller.php'</span> ))&#123;</div><div class="line">    <span class="keyword">require_once</span> APP_ROOT . <span class="string">'controller/'</span> . $controller.<span class="string">'Controller.php'</span>;</div><div class="line">    $obj=<span class="keyword">new</span> $controller();</div><div class="line">    <span class="keyword">if</span> (method_exists($obj,$action))&#123;</div><div class="line">        $obj-&gt;$action();</div><div class="line">    &#125;<span class="keyword">else</span>&#123;</div><div class="line">        <span class="keyword">die</span>(<span class="string">'func not exist'</span>);</div><div class="line">    &#125;</div><div class="line">&#125;<span class="keyword">else</span>&#123;</div><div class="line">    <span class="keyword">die</span>(<span class="string">'file not exist'</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="创建测试控制器"><a href="#创建测试控制器" class="headerlink" title="创建测试控制器"></a>创建测试控制器</h1><p>我们在<code>controller</code>文件夹里面创建一个<code>IndexController.php</code> 文件：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">IndexController</span></span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">echo</span> <span class="string">'test data'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>试一试 <code>localhost/index.php?c=index&amp;a=test</code> ，不出意外就能得到<code>test data</code> 了。</p>
<h1 id="提取父类控制器"><a href="#提取父类控制器" class="headerlink" title="提取父类控制器"></a>提取父类控制器</h1><p>光是这样肯定是不够的，因为我们需要在controller里面连接model，调用模板，加载各种工具类等。因此我们需要一个带一些公用方法的<br>父类，这个父类可以被所有的controller继承。</p>
<p>我们创建一个<code>controller.php</code>文件：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">controller</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="comment">//打印的方法</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">dump</span><span class="params">($data)</span></span>&#123;</div><div class="line">        <span class="keyword">echo</span> <span class="string">'&lt;p&gt;'</span>;</div><div class="line">        print_r($data);</div><div class="line">        <span class="keyword">echo</span> <span class="string">'&lt;/p&gt;'</span>;</div><div class="line">        <span class="keyword">die</span>;</div><div class="line">    &#125; </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这样一个简单的父类就完成了，里面带了一个很常用的格式化打印数据的方法。那么这个父类我们放在哪里呢？ 最简单的方法就是放在<br><code>application/controller</code>文件夹里面了，但是一般来说application文件夹是用来放我们项目本身的代码的，框架的代码我们最好放在<br><code>system</code> 文件夹里面,然后通过<code>__autoload()</code> 函数自动调用。</p>
<p>我们可以在index.php 里面添加这个函数:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">__autoload</span><span class="params">($className)</span></span>&#123;</div><div class="line">    <span class="keyword">if</span>(file_exists(BASE_ROOT . <span class="string">'system/'</span> . $className . <span class="string">'.php'</span>))&#123;</div><div class="line">        <span class="keyword">require_once</span> BASE_ROOT . <span class="string">'system/'</span> . $className . <span class="string">'.php'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>接下来我们就能在项目的controller继承这个父类了：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">IndexController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">$this</span>-&gt;dump(<span class="string">'test data'</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="Model"><a href="#Model" class="headerlink" title="Model"></a>Model</h1><p>接下来我们在application文件夹下面创建model文件夹，用来存放模型。那怎么通过controller来调用model？自然就是通过父类自带<br>的方法了。我们在<code>controller.php</code>里面添加一个方法：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">model</span><span class="params">($modelName)</span></span>&#123;</div><div class="line">    $modelFile = $modelName . <span class="string">'Model'</span>;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">new</span> $modelFile();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>单是这样还不行，因为php并不知道我们试图实例化的这个model文件在哪里，因此我们需要通过<code>__autoload()</code>函数告诉PHP：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">__autoload</span><span class="params">($className)</span></span>&#123;</div><div class="line">    <span class="keyword">if</span>(file_exists(BASE_ROOT . <span class="string">'system/'</span> . $className . <span class="string">'.php'</span>))&#123;</div><div class="line">        <span class="keyword">require_once</span> BASE_ROOT . <span class="string">'system/'</span> . $className . <span class="string">'.php'</span>;</div><div class="line">    &#125;<span class="keyword">elseif</span>(file_exists(APP_ROOT . <span class="string">'model/'</span> . $className . <span class="string">'.php'</span>))&#123;</div><div class="line">        <span class="keyword">require_once</span> APP_ROOT . <span class="string">'model/'</span> . $className . <span class="string">'.php'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>然后我们就可以写一个测试用的<code>TestModel.php</code>了：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestModel</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">findData</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="string">'model data'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>试着在<code>TestController.php</code> 里面调用看看：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">IndexController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span>&#123;</div><div class="line">        $testModel = <span class="keyword">$this</span>-&gt;model(<span class="string">'Test'</span>);</div><div class="line">        <span class="keyword">echo</span> $testModel-&gt;findData();   <span class="comment">//输出 model data；</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="连接数据库"><a href="#连接数据库" class="headerlink" title="连接数据库"></a>连接数据库</h1><p>但是现在的model是无法连接数据库的，这样的model就是个废物。但是在每一个model里面写连接数据库的方法是很蠢的，最好的<br>方法就是通过父类的model连接数据库，其他model继承父类就好了。</p>
<p>和controller.php相同，我们在<code>sysytem</code>文件夹下创建一个<code>model.php</code>  ：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">model</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">protected</span> $_db;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">$this</span>-&gt;_db=<span class="keyword">new</span> mysqli(<span class="string">'localhost'</span>, <span class="string">'root'</span>,<span class="string">'123456'</span>, <span class="string">'myphp'</span>);</div><div class="line">        <span class="keyword">if</span>(mysqli_connect_errno())&#123;</div><div class="line">            <span class="keyword">echo</span> <span class="string">'error...'</span>.mysqli_connect_errno();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">query</span><span class="params">($sql)</span></span>&#123;</div><div class="line">        $result=<span class="keyword">$this</span>-&gt;_db-&gt;query($sql);</div><div class="line">        <span class="keyword">if</span>($result-&gt;num_rows)&#123;</div><div class="line">            <span class="keyword">while</span> ($row = $result-&gt;fetch_assoc()) &#123;</div><div class="line">                $myRow[] = $row;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> $myRow;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>自然我们所有的model都继承这个父类model：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestModel</span> <span class="keyword">extends</span> <span class="title">Model</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="comment">// other code......</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>让我们在本地建立一个<code>myphp</code>数据库，再新建一个<code>user</code>表，格式如下：</p>
<table>
<thead>
<tr>
<th style="text-align:left">id</th>
<th style="text-align:left">name</th>
<th style="text-align:left">age</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">zhang</td>
<td style="text-align:left">20</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left">shao</td>
<td style="text-align:left">21</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">wang</td>
<td style="text-align:left">22</td>
</tr>
</tbody>
</table>
<p>让我们写一个<code>UserController</code>连接<code>UserModel</code>查找数据，所以我们要这么做：</p>
<p>在<code>application/controller</code> 文件夹下面创建<code>UserController.php</code>,内容如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">user</span><span class="params">()</span></span>&#123;</div><div class="line">        $id = $_GET[<span class="string">'id'</span>];</div><div class="line">        $userModel = <span class="keyword">$this</span>-&gt;model(<span class="string">'User'</span>);</div><div class="line">        $user = $userModel-&gt;getUser($id);</div><div class="line">        <span class="keyword">$this</span>-&gt;dump($user);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在<code>application/model</code> 文件夹下面创建<code>UserModel.php</code>,内容如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserModel</span> <span class="keyword">extends</span> <span class="title">Model</span></span></div><div class="line"><span class="class"></span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUser</span><span class="params">($id)</span></span>&#123;</div><div class="line">        $sql = <span class="string">'select * from user where id ='</span>.$id;</div><div class="line">        $data = <span class="keyword">$this</span>-&gt;query($sql);</div><div class="line">        <span class="keyword">return</span> $data;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>试着通过<code>localhost/index.php?c=user&amp;a=user&amp;id=1</code> 请求看看吧！</p>
<p>待续～～～</p>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/create-a-php-framework.html">http://notes.wen.moe/post/create-a-php-framework.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/技术/">技术</a>
            
              <a href="/tags/PHP/">PHP</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/learn-japanese-class-one.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">杂鱼君学日语--假名和发音</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/post/install-lnmp-on-elementary-os.html">
        <span class="next-text nav-default">在elementary os 上安装LNMP</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/create-a-php-framework.html';
        this.page.identifier = 'post/create-a-php-framework.html';
        this.page.title = '从零开始写一个PHP框架';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
